package com.welink.cmp.adapter.api.metadata;

import com.welink.base.catchlog.CatchAndLog;
import com.welink.base.exception.BizException;
import com.welink.cmp.dto.ErrorCode;
import com.welink.cmp.dto.metadata.DestroyUpdateCmd;
import com.welink.cmp.dto.metadata.switchs.*;
import com.welink.cmp.meatdata.service.SwitchAppService;
import com.welink.common.dto.Response;
import com.welink.common.dto.SingleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 交换机生命周期管理接口
 *
 * @author yanwei li
 * @create 2024-05-23
 */
@RestController
@RequestMapping("/api/switch")
@CatchAndLog
public class SwitchLifeCycleServiceApi {

    @Autowired
    private SwitchAppService switchService;

    /**
     * 新增交换机
     *
     * @param swAddCmd 创建指令
     * @return ID
     */
    @PostMapping("/addSwitch")
    public SingleResponse<Long> addSwitch(@RequestBody CreateSwitchAddCmd swAddCmd) {
        final Long id = switchService.addSwitch(swAddCmd);
        return SingleResponse.of(id);
    }

    /**
     * 启用交换机
     *
     * @param launchSwUpdCmd 启用指令
     * @return 操作结果码
     */
    @PostMapping("/launchSwitch")
    public Response launchSwitch(@RequestBody LaunchSwitchUpdateCmd launchSwUpdCmd) {
        // TODO 状态机
        final Boolean result = switchService.launchSwitch(launchSwUpdCmd);
        if (!result){
            throw new BizException("启动交换机异常");
        }
        return Response.buildSuccess();
    }
    /**
     * [修改]配置交换机基础信息
     * @param setupBaseSwitchUpdCmd cmd
     * @return resultCode
     */
    @PostMapping("/setup/baseSwitch")
    public Response setupSwitchBaseInfo(@RequestBody SetupSwitchBaseUpdateCmd setupBaseSwitchUpdCmd){
        final Boolean b = switchService.setupSwitchBaseInfo(setupBaseSwitchUpdCmd);
        if (!b){
            return Response.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"配置交换机基础信息失败");
        }
        return Response.buildSuccess();
    }
    /**
     * [修改]配置交换机物理信息
     * @param setupSwitchRackUpdCmd cmd
     * @return  resultCode = 0 成功 非 0 即错误码和错误信息
     */
    @PostMapping("/setup/rackSwitch")
    public Response setupSwitchRackInfo(@RequestBody SetupSwitchRackUpdateCmd setupSwitchRackUpdCmd){
        final Boolean b = switchService.setupSwitchRackInfo(setupSwitchRackUpdCmd);
        if (!b){
            return Response.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"配置交换机物理信息失败");
        }
        return Response.buildSuccess();
    }

    /**
     * [修改]配置交换机网络信息
     * @param setupSwitchNetUpdCmd 配置交换机
     * @return  resultCode = 0 成功 非 0 即错误码和错误信息
     */
    @PostMapping("/setup/netSwitch")
    public Response setupSwitchNetInfo(@RequestBody SetupSwitchNetUpdateCmd setupSwitchNetUpdCmd){
        final Boolean b = switchService.setupSwitchNetInfo(setupSwitchNetUpdCmd);
        if (!b){
            return Response.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"配置交换机网络信息信息失败");
        }
        return Response.buildSuccess();
    }

    /**
     * 禁用交换机
     *
     * @param shutdownSwUpdCmd 禁用指令
     * @return 操作结果码
     */
    @PostMapping("/shutdownSwitch")
    public Response shutdownSwitch(@RequestBody ShutdownSwitchUpdateCmd shutdownSwUpdCmd) {
        final Boolean b = switchService.shutdownSwitch(shutdownSwUpdCmd);
        if (!b){
            return Response.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"下架防火墙发生异常");
        }
        return Response.buildSuccess();
    }

    /**
     * [修改]注销交换机
     * @param destroyUpdCmd 下架交换机
     * @return  resultCode = 0 成功 非 0 即错误码和错误信息
     */
    @PostMapping("/destroySwitch")
    public Response destroySwitch(@RequestBody DestroyUpdateCmd destroyUpdCmd){
        // TODO 状态机
        return Response.buildSuccess();
    }
}